/**
 * Created by rockyren on 15/1/1.
 */
orderApp.controller('orderIndexController', ['$scope', '$location', 'orderServices', 'cartServices', function($scope, $location, orderServices, cartServices){
  cartServices.get()
    .success(function(data){
      $scope.products = data;
      $scope.total = 0;
      $scope.productIdList = [];
      for(var i=0; i<$scope.products.length; i++){
        var product = $scope.products[i];
        $scope.productIdList.push(product.id);
        $scope.total += product.price * product.count;
      }
    })
    .error(function(data){
      console.log(data.error);
    });

  $scope.$on('set:address', function(e, curAddress){

    console.log($scope.curAddress);
    $scope.curAddress = curAddress;
  });

  $scope.submitOrder = function(){
    if(!$scope.curAddress){
      alert('请选择一个地址');
    }else {
      var curDistrict = $scope.curAddress.district;
      var curAddress = {
        receiver: $scope.curAddress.name,
        phone: $scope.curAddress.phone,
        postcode: $scope.curAddress.postcode,
        deliverAddress: curDistrict.province.name + curDistrict.city.name + curDistrict.region.name
        + ' ' + $scope.curAddress.location
      };
      orderServices.store({
        address: curAddress,
        productList: $scope.products,
        total: $scope.total
      })
        .success(function(){
          $location.path('/success')
          console.log('提交订单成功');
        })
        .error(function(){
          console.log('提交订单失败');
        });
    }


  };


}]);

orderApp.controller('orderShowController', ['$scope', 'orderServices', '$routeParams', function($scope, orderServices, $routeParams){
  /*
  function loadSingleOrder() {
    orderServices.show($routeParams.orderId)
      .success(function(data){
        $scope.orderInfo = data;
      })
      .error(function(data){

      });
  }
  loadSingleOrder();
*/
}]);